// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var leftMenu = require("../leftMenu/leftMenu");
    var topMenu = require("../topMenu/topMenu");
    var footer = require("../footer/footer_main");
    var router = require("../hash/pageRouter");
    var dialog = require("dialog");
    var validate = require("validate");
    var pageIndex = 1,
        pageSize = 10,
        totalPageCount = 1,
        totalRecordCount = 0; //分页初始化
    var imgSrc;
    var init = function(num){
    	footer.init();  
    	$("#footer").removeClass("none");
    	leftMenu.init(num);
    	topMenu.init(7);
    	router.init();
    	$(".table-section").removeClass("none");
    	$(".footer").removeClass("none");
		initEvent();
//  	$("#organization").parent().addClass("leftMenu-active");
//		router.route("organization/organization.html");
    };
	var initEvent = function(){
		//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			location.href="../login/login.html";
		}
		
		/*新增的点击事件*/
		$("#leftMenu").on("click", "#paramManage", function(){
			router.route("systemParamManage/paramManage.html");
		});
		
		$("#leftMenu").on("click", "#sampleManage", function(){
			router.route("sampleManage/sampleManage.html");
		});
		$("#leftMenu").on("click", "#costtypes", function(){
			router.route("costtypess/costtypes.html");
		});
        $("#leftMenu").on("click","#permission-manage",function(){
            router.route("permisionManage/permisionManage.html");
        });
        
		$("#leftMenu").on("click", "#announcement-manager", function() {
            router.route("announceManage/platform_main_announcement_manager.html");
        });
        $("#leftMenu").on("click", "#advertisement-manager", function() {
            router.route("adManage/platform_main_advertisement_manager.html");
        });
        $("#leftMenu").on("click", "#organization", function() {
            router.route("organization/organization.html");
        });
        $("#leftMenu").on("click", "#memberGrade", function() {
            router.route("memberGrade/memberGrade.html");
        });
        $("#leftMenu").on("click", "#productManage", function() {
            router.route("productManage/productManage.html");
        });
        $("#leftMenu").on("click", "#constractLogisticsManage", function() {
            router.route("constractLogisticsManage/constractLogisticsManage.html");
        });
        $("#leftMenu").on("click", "#constractSupervisionManage", function() {
            router.route("constractSupervisionManage/constractSupervisionManage.html");
        });
    };
    
	//增加广告
	var newad = function() {
		var promptDialog = validate.validateInput($("#add_ad_dialog"));
		if (promptDialog.length != 0) {
			util.alert(promptDialog);
		} else {
			var body = base.parseForm($("#add_ad_dialog"));
			body.identify = $("#identify").val();
			body.name = $("#name").val();
			body.description = $("#ad_describe").val();
			body.length = $("#length").val();
			body.width = $("#width").val();
			body.example = imgSrc;
			$(".footerBtn").on("click", "#sureUpdFirst", function() {
				base.fnAjax({
					action: "createAdSpace.json", //接口名称
					async: false, //同步异步
					nocode: false, //是否要登陆，true为不用登陆（其他页面不用传该参数，默认为false）
					oData: body, //传递参数
					fnSuccess: function(data) { //成功回调
						if (data.status == 200) {
	//						base.openUrl("../adManage/adManage.html")
						} else {
							base.alert(3, data.message);
						}
						dialog.hideDialog($("#add_ad_dialog"));
					}
				});
			});
		}
	};
	
    //查询广告位列表
    var inquireAdSpaceList = function(){
        var keyword = $("#keyword").val();
        base.fnAjax({
            action: "inquireAdSpaceList.json",
            async: false, //同步异步
			nocode: false, //是否要登陆，true为不用登陆
            oData: {
				name: "",
				version: "v0",
				identify: "",
				pageIndex: pageIndex,
				pageSize: pageSize
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                    var tpl = $("#tbody_data_tpl").html();
                    var html = juicer(tpl, data);
                    $("#adManage_tbody").html(html);
                    
                    totalPageCount = data.totalPageCount;
                    totalRecordCount = data.totalRecordCount;
                    base.pageControl($("#pagenation"),
                    totalRecordCount,
                    data.pageIndex,
                    data.pageSize,
                    function(p) {
                        pageIndex = p;
                        inquireAdSpaceList();
                    });
                } else {
                    base.alert(2,"信息获取失败");
                }
            }
        });
    };
    
    return {
        init:init
    };
});